package com.atguigu.mycloud.controller;

import com.atguigu.mycloud.service.PayFeignAPI;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 任浩
 * @version 1.0
 */

@RestController
public class OrderCiruitController {

    @Resource
    private PayFeignAPI payFeignAPI;

    @GetMapping(value = "/feign/pay/circuit/{id}")
    @CircuitBreaker(name = "cloud-payment-service", fallbackMethod = "myCircuitFallback")
    public String myCircuitBreaker(@PathVariable("id") Integer id)
    {
        return payFeignAPI.myCircuit(id);
    }
    //myCircuitFallback就是服务降级后的兜底处理方法
    public String myCircuitFallback(Integer id,Throwable t) {
        // 这里是容错处理逻辑，返回备用结果
        return "myCircuitFallback，系统繁忙，请稍后再试-----/(ㄒoㄒ)/~~";
    }

    //=========Resilience4j bulkhead 的例子
    @GetMapping(value = "/feign/pay/bulked/{id}")
    @Bulkhead(name = "cloud-payment-service",fallbackMethod = "myBulkheadFallback",type = Bulkhead.Type.SEMAPHORE)
    public String myBulkhead(@PathVariable("id") Integer id)
    {
        return payFeignAPI.myBulkhead(id);
    }
    public String myBulkheadFallback(Throwable t)
    {
        return "myBulkheadFallback，隔板超出最大数量限制，系统繁忙，请稍后再试-----/(ㄒoㄒ)/~~";
    }

    //=========Resilience4j ratelimit 的例子
    @GetMapping(value = "/feign/pay/ratelimit/{id}")
    @RateLimiter(name="cloud-payment-service",fallbackMethod = "myRateLimiterFallback")
    public String myRateLimit(@PathVariable("id") Integer id){
       return payFeignAPI.myRatelimit(id);
    }
    public String myRateLimiterFallback(Integer id,Throwable t){
        return "myRateLimiterFallback，限流超出最大数量限制，系统繁忙，请稍后再试-----/(ㄒoㄒ)/~~";
    }

}
